import { Controller, Get, Post, Body, Param, Delete, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import { TagsService } from './tags.service';
import { TagResponseDto } from './dto/tag-response.dto';
import { ContentTagMappingResponseDto } from './dto/content-tag-mapping-response.dto';
import { AddContentTagDto } from './dto/add-content-tag.dto';
import { UuidValidationPipe } from '../common/pipes/uuid-validation.pipe';

@ApiTags('Tag')
@Controller('contents/:contentId/tags')
export class ContentTagsController {
  constructor(private readonly tagsService: TagsService) {}

  @Get()
  @ApiOperation({ summary: '查询指定内容的标签列表', description: '获取指定内容关联的所有标签' })
  @ApiParam({ name: 'contentId', description: '内容的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 200, description: '返回该内容的标签列表', type: [TagResponseDto] })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  getContentTags(
    @Param('contentId', UuidValidationPipe) contentId: string,
  ): Promise<TagResponseDto[]> {
    return this.tagsService.getContentTags(contentId);
  }

  @Post()
  @ApiOperation({ summary: '为内容添加标签', description: '为指定内容添加标签映射' })
  @ApiParam({ name: 'contentId', description: '内容的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 201, description: '标签映射创建成功', type: ContentTagMappingResponseDto })
  @ApiResponse({ status: 404, description: '内容或标签不存在' })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  async addContentTag(
    @Param('contentId', UuidValidationPipe) contentId: string,
    @Body() addContentTagDto: AddContentTagDto,
  ): Promise<ContentTagMappingResponseDto> {
    return this.tagsService.addContentTag(contentId, addContentTagDto.tagId);
  }

  @Delete(':tagId')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '从内容中移除标签', description: '删除指定内容和标签之间的映射关系' })
  @ApiParam({ name: 'contentId', description: '内容的唯一标识', format: 'uuid' })
  @ApiParam({ name: 'tagId', description: '标签的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 204, description: '标签映射删除成功' })
  @ApiResponse({ status: 404, description: '映射关系不存在或未找到' })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  removeContentTag(
    @Param('contentId', UuidValidationPipe) contentId: string,
    @Param('tagId', UuidValidationPipe) tagId: string,
  ): Promise<void> {
    return this.tagsService.removeContentTag(contentId, tagId);
  }
}
